import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:go_pet/src/features/menu/view/widgets/content.dart';
import 'package:go_pet/src/features/menu/view/widgets/post_pet.dart';
import 'package:go_pet/src/features/profile/account_screen.dart';
import 'package:go_pet/src/features/login/view/widgets/pages/login_screen.dart';
import 'package:go_pet/src/theme/app_colors.dart';
import 'package:go_pet/src/theme/image_sources.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  late int _selectedIndex = 0;
  static const List<Widget> _pages = <Widget>[
    Content(),
    AccountScreen(),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    final user = FirebaseAuth.instance.currentUser;
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        title: Image.asset(
          ImageSources.appname,
          height: 30,
        ),
      ),
      body: SafeArea(
        child: Center(
          child: (user == null)
              ? const Content()
              : _pages.elementAt(_selectedIndex),
        ),
      ),
      floatingActionButton: user != null
          ? SizedBox(
              height: 45,
              width: 190,
              child: FloatingActionButton.extended(
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => const PostPet()),
                  );
                },
                icon: const Icon(Icons.pets),
                label: Text(
                  'Создать Запись',
                  style: Theme.of(context).textTheme.labelMedium,
                ),
                foregroundColor: AppColors.white,
                backgroundColor: AppColors.primary,
                hoverColor: AppColors.white,
              ),
            )
          : SizedBox(
              height: 45,
              width: 99,
              child: FloatingActionButton.extended(
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => const LoginScreen()),
                  );
                },
                icon: const Icon(Icons.login),
                label: Text(
                  'Войти',
                  style: Theme.of(context).textTheme.labelMedium,
                ),
                foregroundColor: AppColors.white,
                backgroundColor: AppColors.primary,
                hoverColor: AppColors.white,
              ),
            ),
      bottomNavigationBar: user != null
          ? BottomNavigationBar(
              backgroundColor: AppColors.white,
              selectedItemColor: AppColors.primary,
              unselectedItemColor: const Color.fromARGB(255, 187, 187, 187),
              items: const <BottomNavigationBarItem>[
                BottomNavigationBarItem(
                  icon: Icon(Icons.contact_page),
                  label: 'Лента',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.person),
                  label: 'Профиль',
                ),
              ],
              currentIndex: _selectedIndex,
              onTap: _onItemTapped,
            )
          : const SizedBox(),
    );
  }
}
